<?php

namespace app\store\controller;

use app\common\model\video\Course as CourseModel;
use app\common\model\video\CourseCate;
use app\common\model\video\CoursePart;
use app\Request;
use app\store\controller\Controller;
use think\exception\ValidateException;

/**
 * 课程资源
 */
class Video extends Controller
{
    protected $service;

    public function __construct()
    {
        parent::__construct();
        $this->service = new \app\common\service\Course();
    }

    //课程列表
    public function list()
    {
//        $where = $request->getMore([
//            ['cate_id', 0],
//            ['status', 0],
//            ['name', '']
//        ]);
        $cate = $this->service->cates();
        $list = $this->service->lists($this->request->param(), ['cate','image','part']);
//        dump($list->toArray());die;
        return $this->fetch('index', compact('list','cate'));
    }


    //课程详情
    public function detail($id)
    {
        $data = $this->service->detail($id, 0);
        return $this->fetch('detail', compact('data'));
    }


    //分类
    public function cateList()
    {
        $list = $this->service->cates();
        return $this->fetch('cate', compact('list'));
    }

    /**
     * 添加分类
     */
    public function addCate()
    {
        if (!$this->request->isAjax()) {
            return $this->fetch(
                'add', []
            );
        }
        if ($this->service->addCate($this->request->post('category/a'))) {
            return $this->renderSuccess('添加成功', url('shop.video/catelist'));
        }
        return $this->renderError('添加失败');
    }

    /**
     * 编辑分类
     * @param int $id
     */
    public function editCate(int $id)
    {
        $model = CourseCate::get(['id' => $id]);
        if (!$this->request->isAjax()) {
            return $this->fetch(
                'edit', compact('model')
            );
        }

        if ($this->service->editCate($id, $this->request->post('category/a'))) {
            return $this->renderSuccess('编辑成功', url('shop.video/catelist'));
        }
        return $this->renderError('编辑失败');
    }

    /**
     * 删除分类
     * @param int $id
     */
    public function delCate(int $id)
    {
        if ($this->service->delCate($id)) {
            return $this->renderSuccess('删除成功', '');
        }
        return $this->renderError('删除失败');
    }

    /**
     * 添加课程
     * @return
     */
    public function addCourse()
    {
        if (!$this->request->isAjax()){
            $cate = $this->service->cates();
            return  $this->fetch('add_course',compact('cate'));
        }
        if ($this->service->addCourse($this->postData('video'))) {
            return $this->renderSuccess('添加成功',url('shop.video/list'));
        }
        return $this->renderError('添加失败');
    }

    /**
     * 添加课程
     * @param int $id
     * @return
     */
    public function editCourse(int $id)
    {
        $model = CourseModel::detail($id);
        if (!$this->request->isAjax()){
            $cate = $this->service->cates();
            return  $this->fetch('edit_course',compact('cate','model'));
        }

        if ($this->service->editCourse($id, $this->postData('video'))) {
            return $this->renderSuccess('编辑成功',url('shop.video/list'));
        }
        return $this->renderError('编辑失败');
    }

    /**
     * 删除课程
     * @param int $id
     * @return
     */
    public function delCourse(int $id)
    {
        if ($this->service->delCourse($id)) {
            return $this->renderSuccess( '删除成功',url('shop.video/index'));
        }
        return $this->renderError('删除失败');
    }

    public function state(){
        if (!$this->service->stateCourse($this->postData())) {
            return $this->renderError('操作失败');
        }
        return $this->renderSuccess('操作成功');
    }

    /**
     * @title 章节列表
     * @param int $c_id
     * @author Qian
     * Time: 2023/4/6  15:29
     */
    public function partList()
    {
        $list = $this->service->listPart($this->request->param());
        $video = (new CourseModel())->getAllList();
        return $this->fetch('part_list',compact('list','video'));
    }

    /**
     * 添加章节
     * @return
     */
    public function addPart()
    {
        if (!$this->request->isAjax()){
            $video = (new CourseModel())->getAllList();
            return  $this->fetch('add_part',compact('video'));
        }
        if ($this->service->addPart($this->postData('part'))) {
            return $this->renderSuccess('添加成功',url('shop.video/partList'));
        }
        return $this->renderError('添加失败');
    }

    /**
     * 编辑章节
     * @param int $id
     * @return
     */
    public function editPart(int $id)
    {
        if (!$this->request->isAjax()){
            $model = CoursePart::detail($id);
            $video = (new CourseModel())->getAllList();
            return  $this->fetch('edit_part',compact('video','model'));
        }
        if ($this->service->editPart($id, $this->postData('part'))) {
            return $this->renderSuccess('编辑完成',url('shop.video/partList'));
        }
        return $this->renderError('编辑失败');
    }

    /**
     * 删除章节
     * @param int $id
     * @return
     */
    public function delPart(int $id)
    {
        if ($this->service->delPart($id)) {
            return $this->renderSuccess('删除成功',url('shop.video/partList'));
        }
        return $this->renderError('删除失败');
    }
}
